﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Aribaccio
{
    /// <summary>
    /// 定义扩展Enum枚举类型类
    /// </summary>
    public static class ExtendEnum
    {
        /// <summary>
        /// 数字转换为枚举类型
        /// </summary>
        /// <typeparam name="T">泛型枚举类型</typeparam>
        /// <param name="argEnumKey">枚举值</param>
        /// <returns></returns>
        public static T ToEnum<T>(this int argEnumKey) where T : Enum
        {
            if (Enum.IsDefined(typeof(T), argEnumKey))
            {
                return (T)Enum.ToObject(typeof(T), argEnumKey);
            }
            return default;
        }

        /// <summary>
        /// 字符串转换为枚举类型
        /// </summary>
        /// <typeparam name="T">泛型枚举类型</typeparam>
        /// <param name="argEnumName">枚举值</param>
        /// <returns>枚举类型</returns>
        public static T ToEnum<T>(this string argEnumName) where T : Enum
        {
            if (string.IsNullOrWhiteSpace(argEnumName)) { return default; }
            if (Enum.IsDefined(typeof(T), argEnumName))
            {
                return (T)Enum.Parse(typeof(T), argEnumName);
            }
            return default;
        }

        /// <summary>
        /// 枚举类型转换为数字
        /// </summary>
        /// <typeparam name="T">泛型枚举类型</typeparam>
        /// <param name="argEnum">枚举值</param>
        /// <returns>枚举值</returns>
        public static int ToInt<T>(this T argEnum) where T : Enum
        {
            if (argEnum == null) { return 0; }
            return (int)((object)argEnum);
        }

        /// <summary>
        /// 枚举类型转换为字符串
        /// </summary>
        /// <typeparam name="T">泛型枚举类型</typeparam>
        /// <param name="argEnum">枚举值</param>
        /// <returns>枚举值字符串</returns>
        public static string ToStringName<T>(this T argEnum) where T : Enum
        {
            if (argEnum == null) { return string.Empty; }
            string? strEnumName = Enum.GetName(argEnum.GetType(), argEnum);
            return strEnumName == null ? string.Empty : strEnumName;
        }

        /// <summary>
        /// 获取枚举字段的注释[DisplayName特性]
        /// </summary>
        /// <param name="argEnum">枚举值</param>
        /// <returns></returns>
        public static string ToDisplayName<T>(this T argEnum) where T : Enum
        {
            if (argEnum == null) { return string.Empty; }
            var type = argEnum.GetType();
            var item = type.GetField(argEnum.ToString(), BindingFlags.Public | BindingFlags.Static);
            //需要判断是否为null
            if (item != null)
            {
                var att = item.GetCustomAttribute<DisplayNameAttribute>(false);
                if (att != null && !string.IsNullOrWhiteSpace(att.DisplayName)) { return att.DisplayName; }
            }
            return string.Empty;
        }

        /// <summary>
        /// 获取枚举字段的注释[Description特性]
        /// </summary>
        /// <param name="argEnum">枚举值</param>
        /// <returns></returns>
        public static string ToDescription<T>(this T argEnum) where T : Enum
        {
            if (argEnum == null) { return string.Empty; }
            var type = argEnum.GetType();
            var item = type.GetField(argEnum.ToString(), BindingFlags.Public | BindingFlags.Static);
            //需要判断是否为null
            if (item != null)
            {
                var att = item.GetCustomAttribute<DescriptionAttribute>(false);
                if (att != null && !string.IsNullOrWhiteSpace(att.Description)) { return att.Description; }
            }
            return string.Empty;
        }
    }
}
